﻿using System;
using System.Collections.Generic;
using System.Linq;

namespace Game.Plugin.Parse
{
    public class Variable : IExpression 
    {
        readonly string _Name;

        public Variable(string name)
        {
            _Name = name;
        }

        public object Interpret(Dictionary<String, ICodeObject> variables)  
        { 
            if (!variables.ContainsKey(_Name))
            {
                return 0;
            }

            return variables[_Name].Interpret(variables); 
        }
    }

    public class VariableParser : IParser
    {
        public bool CanParse(string data)
        {
            return data.Length > 0 && data.All(char.IsLower);
        }

        public ICodeObject Parse(
            string token, 
            ParserContext context)
        {
            IExpression e = new Variable(token);
            context.Stack.Push(e);
            return e;
        }

    }
}

